package com.sairui.xiaoruilib;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.sairui.xiaoruilib.bean.CardOptionEntity;
import com.sairui.xiaoruilib.bean.MoreAskEntity;
import com.sairui.xiaoruilib.manager.SimpleTitle;
import com.sairui.xiaoruilib.widgets.flowlayout.FlowLayout;
import com.sairui.xiaoruilib.widgets.flowlayout.TagAdapter;
import com.sairui.xiaoruilib.widgets.flowlayout.TagFlowLayout;

import java.lang.ref.WeakReference;
import java.util.Collections;


public class CardOptionActivity extends AppCompatActivity {
    TextView optionTitle;
    TagFlowLayout hotSearch;
    private TagAdapter mHotAdapter;
    private MoreAskEntity moreAskEntity;

    public static Intent getInstance(Context context, MoreAskEntity moreAskEntity) {
        Intent intent = new Intent(context, CardOptionActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", moreAskEntity);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_option);
        SimpleTitle mTitle = new SimpleTitle(new WeakReference<Activity>(this));
        mTitle.setBack();

        optionTitle = findViewById(R.id.option_title);
        hotSearch = findViewById(R.id.hot_search);

        moreAskEntity = (MoreAskEntity) getIntent().getSerializableExtra("data");
        optionTitle.setText(moreAskEntity.getCardTitle());

        Collections.sort(moreAskEntity.gettCardOptionList());
        mHotAdapter = new TagAdapter<CardOptionEntity>(moreAskEntity.gettCardOptionList()) {
            @Override
            public View getView(FlowLayout parent, int position, final CardOptionEntity entity) {
                View view = LayoutInflater.from(CardOptionActivity.this).inflate(R.layout.adapter_search_content_item, parent, false);
                TextView tvItemSpec = (TextView) view.findViewById(R.id.item);
                tvItemSpec.setText("DIGITAL_SERIAL".equals(moreAskEntity.getOptionCardSerialStyleCode()) ?
                        (position + 1) + "." + entity.getOptionItemName() : entity.getOptionItemName());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("key",entity.getOptionItemName());
                        setResult(9527,intent);
                        finish();
                    }
                });
                return view;
            }
        };
        hotSearch.setAdapter(mHotAdapter);
    }

}
